package common;

import java.lang.reflect.Method;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

public abstract class BaseAspect implements MethodInterceptor
{

    @SuppressWarnings("unchecked")
    public <T> T getProxy(Class<T> cls)
    {
        return (T) Enhancer.create(cls, this);
    }

    public final Object intercept(Object proxy, Method methodTarget,
                    Object[] args, MethodProxy methodProxy) throws Throwable
    {
        begin(methodTarget, args);
        Object result = null;
        try
        {
            if (filter(methodTarget, args))
            {
                before(methodTarget, args);
                result = methodProxy.invokeSuper(proxy, args);
                after(methodTarget, args);
            }
            else
            {
                result = methodProxy.invokeSuper(proxy, args);
            }
        }
        catch (Exception e)
        {
            error(methodTarget, args, e);
            throw new RuntimeException(e);
        }
        finally
        {
            end(methodTarget, args);
        }
        return result;
    }

    public void begin(Method method, Object[] args)
    {
    }

    public boolean filter(Method method, Object[] args)
    {
        return true;
    }

    public void before(Method method, Object[] args)
    {
    }

    public void after(Method method, Object[] args)
    {
    }

    public void error(Method method, Object[] args, Exception e)
    {
    }

    public void end(Method method, Object[] args)
    {
    }
}
